package simple;

import Constant.constant;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class RabbitMQ {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建链接工厂
        ConnectionFactory factory=new ConnectionFactory();
        //2.设置参数
        factory.setHost(constant.localhost);
        factory.setPort(constant.port);
        factory.setVirtualHost(constant.VirtuHost);
        factory.setUsername(constant.UserName);
        factory.setPassword(constant.Password);
        //3.创立链接
        Connection connection=factory.newConnection();

        //4.创建channel信道
        Channel channel=connection.createChannel();

        /**queueDeclare(String queue, boolean durable, boolean exclusive, boolean
         autoDelete, Map<String, Object> arguments)
         queue:队列名称
         duarble:是否持久化 持久化的队列会落盘，服务器重启，消息不丢失
         exclusive:是否独占
         autoDelete：当没有Consumer时，自动删除掉

         *
         */

        /**
         * basicPublish(String exchange, String routingKey, AMQP.BasicProperties props,
         * byte[] body)
         * exchange:简单模式下，交换机为默认的“”
         * routingKey:路由名称,routingKey=队列名称
         */


        //如果没有一个hello这样的队列，会自动创建，有则不用创建
        channel.queueDeclare("hello",true,false,false,null);
        String msg="hello world";
        channel.basicPublish("","hello",null,msg.getBytes(StandardCharsets.UTF_8));
        System.out.println(msg+"消息发送成功");
        channel.close();
        connection.close();

    }
}
